package com.lewis.quartz.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lewis.core.annotation.Excel;
import com.lewis.core.annotation.Excel.ColumnType;
import com.lewis.core.base.domain.BaseEntity;
import com.lewis.core.constant.ScheduleConstants;
import com.lewis.core.utils.StringUtil;
import com.lewis.quartz.utils.CronUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * 定时任务调度表 sys_job
 *
 * @author Lewis
 */
@ApiModel("定时任务调度表 sys_job")
@EqualsAndHashCode(callSuper = true)
@Data
public class SysJob extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "任务ID")
	@Excel(name = "任务序号" , cellType = ColumnType.NUMERIC)
	private Long jobId;

	@ApiModelProperty(value = "任务名称")
	@NotBlank(message = "任务名称不能为空")
	@Size(min = 0, max = 64, message = "任务名称不能超过64个字符")
	@Excel(name = "任务名称")
	private String jobName;

	@ApiModelProperty(value = "任务组名")
	@Excel(name = "任务组名")
	private String jobGroup;

	@ApiModelProperty(value = "调用目标字符串")
	@NotBlank(message = "调用目标字符串不能为空")
	@Size(min = 0, max = 500, message = "调用目标字符串长度不能超过500个字符")
	@Excel(name = "调用目标字符串")
	private String invokeTarget;

	@ApiModelProperty(value = "cron执行表达式")
	@NotBlank(message = "Cron执行表达式不能为空")
	@Size(min = 0, max = 255, message = "Cron执行表达式不能超过255个字符")
	@Excel(name = "执行表达式 ")
	private String cronExpression;

	@ApiModelProperty(value = "cron计划策略")
	@Excel(name = "计划策略 " , readConverterExp = "0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行")
	private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

	@ApiModelProperty(value = "是否并发执行（0允许 1禁止）")
	@Excel(name = "并发执行" , readConverterExp = "0=允许,1=禁止")
	private String concurrent;

	@ApiModelProperty(value = "任务状态（0正常 1暂停）")
	@Excel(name = "任务状态" , readConverterExp = "0=正常,1=暂停")
	private String status;

	@ApiModelProperty(value = "创建者")
	private String createBy;

	@ApiModelProperty(value = "创建时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	@ApiModelProperty(value = "更新者")
	private String updateBy;

	@ApiModelProperty(value = "更新时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	@ApiModelProperty(value = "备注")
	private String remark;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getNextValidTime() {
		if (StringUtil.isNotEmpty(cronExpression)) {
			return CronUtils.getNextExecution(cronExpression);
		}
		return null;
	}
}
